package Protobuf;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class ProtoServer {
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		byte[] buf = new byte[1];
		buf[0] = 1;
	    ServerSocket s = new ServerSocket(90);
	    Socket clientSocket = s.accept();
	    System.out.println("Client was accepted");
	    InputStream input = clientSocket.getInputStream();
    	OutputStream output = clientSocket.getOutputStream();
	    ObjectOutputStream outFile = new ObjectOutputStream(new FileOutputStream(args[0]));
	    SensorDataProto.SensorData obj;
	    do {
	    	obj = SensorDataProto.SensorData.parseDelimitedFrom(input);
	        if (obj != null) {
	        	outFile.writeObject(obj);
	        	output.write(buf);
	        }
	    } while (obj != null);
        outFile.close();
        output.close();
        input.close();
	    s.close();
	}
}
